import { EventTypeEnum, StagePlayerSourceTypeEnum } from 'src/common/constant/enums';
import { StageEvent } from 'src/modules/stage-event/entities/stage-event.entity';
import {
  IsNumber,
  IsOptional,
  IsString,
  IsDate,
  IsBoolean,
  IsArray,
  ValidateNested,
  IsIn,
} from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
import { Event } from 'src/modules/event/entities/event.entity';
import { CreateStageEventDto } from 'src/modules/stage-event/dto/create-stage-event.dto';

export class CreateStageDto {
  @ApiProperty({ description: '阶段 ID', required: false })
  @IsOptional()
  @IsNumber()
  id?: number;

  @ApiProperty({ description: '阶段类型', required: false })
  @IsOptional()
  @IsNumber()
  type?: number;

  @ApiProperty({ description: '阶段名称', required: false })
  @IsOptional()
  @IsString()
  name?: string;

  @ApiProperty({ description: '玩家数量', required: false })
  @IsOptional()
  @IsNumber()
  @Transform(({ value }) => {
    // 尝试将值转换为数字
    const num = Number(value);
    // 若转换结果为 NaN，则返回原始值，否则返回转换后的数字
    return isNaN(num) ? value : num;
  })
  playerCount?: number;

  @ApiProperty({ description: '淘汰赛获胜者数量', required: false })
  @IsOptional()
  @IsNumber()
  @Transform(({ value }) => {
    // 尝试将值转换为数字
    const num = Number(value);
    // 若转换结果为 NaN，则返回原始值，否则返回转换后的数字
    return isNaN(num) ? value : num;
  })
  eliminationWinnerCount?: number;

  @ApiProperty({ description: '轮次名称类型', required: false })
  @IsOptional()
  @IsNumber()
  // roundNameType?: number;
  @ApiProperty({ description: '轮次分组数量', required: false })
  @IsOptional()
  @IsNumber()
  @Transform(({ value }) => {
    // 尝试将值转换为数字
    const num = Number(value);
    // 若转换结果为 NaN，则返回原始值，否则返回转换后的数字
    return isNaN(num) ? value : num;
  })
  roundGroupCount?: number;

  @ApiProperty({ description: '轮次分组获胜者数量', required: false })
  @IsOptional()
  @IsNumber()
  @Transform(({ value }) => {
    // 尝试将值转换为数字
    const num = Number(value);
    // 若转换结果为 NaN，则返回原始值，否则返回转换后的数字
    return isNaN(num) ? value : num;
  })
  roundGroupWinnerCount?: number;

  // @ApiProperty({ description: '轮次循环次数', required: false })
  // @IsOptional()
  // @IsNumber()
  // roundLoopCount?: number;
  // @ApiProperty({ description: '轮次组织类型', required: false })
  // @IsOptional()
  // @IsNumber()
  // roundOrganizationType?: number;
  @ApiProperty({ description: '创建者 ID', required: false })
  @IsOptional()
  @IsNumber()
  createBy?: number;

  @ApiProperty({ description: '创建时间', required: false })
  @IsOptional()
  @IsDate()
  createTime?: Date;

  @ApiProperty({ description: '更新者 ID', required: false })
  @IsOptional()
  @IsNumber()
  updateBy?: number;

  @ApiProperty({ description: '更新时间', required: false })
  @IsOptional()
  @IsDate()
  updateTime?: Date;

  @ApiProperty({ description: '关联事件 ID', required: false })
  @IsOptional()
  @IsNumber()
  fkEventId?: number;

  @ApiProperty({
    description: '事件类型',
    required: false,
    enum: EventTypeEnum,
  })
  @IsOptional()
  eventType?: EventTypeEnum;

  @ApiProperty({
    description: '阶段事件列表',
    required: false,
    type: [CreateStageEventDto],
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateStageEventDto)
  stageEvents?: Array<CreateStageEventDto>;

  @ApiProperty({ description: '回合规则 ID', required: false })
  @IsOptional()
  @IsNumber()
  rallyRuleId?: number;

  @ApiProperty({ description: '比赛规则 ID', required: false })
  @IsOptional()
  @IsNumber()
  gameRuleId?: number;

  @ApiProperty({
    description: '是否必须完成最大局数',
    required: false,
    default: false,
  })
  @IsOptional()
  @IsBoolean()
  mustFinishMaxCountGame?: boolean;

  @ApiProperty({
    description: '是否必须完成最大场数',
    required: false,
    default: false,
  })
  @IsOptional()
  @IsBoolean()
  mustFinishMaxCountMatch?: boolean;

  @ApiProperty({ description: '是否必须签到', required: false, default: false })
  @IsOptional()
  @IsBoolean()
  mustCheckIn?: boolean = false;

  @ApiProperty({ description: '赛事 ID', required: false })
  @IsOptional()
  @IsNumber()
  tournamentId?: number;

  @ApiProperty({ description: '类别 ID', required: false })
  @IsOptional()
  @IsNumber()
  categoryId?: number;

  @ApiProperty({ description: '阶段在事件中的顺序', required: false })
  @IsOptional()
  @IsNumber()
  stageOrderInEvent?: number;

  @ApiProperty({
    description: '玩家来源阶段 ID',
    required: false,
  })
  @IsOptional()
  @IsNumber()
  playerSourceStageId?: number;

  @ApiProperty({
    description: '玩家来源类型，可选值为 winner 或 loser',
    required: false,
    enum: StagePlayerSourceTypeEnum,
  })
  @IsOptional()
  @IsIn(Object.values(StagePlayerSourceTypeEnum))
  playerSourceType?: StagePlayerSourceTypeEnum;

  event: Event;
  playerSource: string;
}
